﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


using System.Reflection;

namespace HA.ModFlow.Application
{
    /// <summary>
    /// ProjectView.xaml 的交互逻辑
    /// </summary>
    public partial class ProjectView : UserControl
    {
        public ProjectView()
        {
            InitializeComponent();

           
            
        }

        ProjectViewModel _viewModel;

        public ProjectViewModel ViewModel
        {
            get { return _viewModel; }
            set { 
                
                _viewModel = value;

                this.DataContext = this._viewModel;

                this.tvProjectNew.ItemsSource = this._viewModel.Children;






                this.tsvDataset.DataContext = this._viewModel.TsvViewModel;

                //Binding bind = new Binding("Visible");

                //bind.Converter = (WPFVisibilityCollapseConvertor)this.FindResource("VisibilityCollapseConvertor");

                //this.tsvDataset.SetBinding(this.tsvDataset.Visibility,bind);


              

                

            
            }
        }




        private void TreeViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
            if (treeViewItem != null)
            {
                treeViewItem.Focus();
                e.Handled = true;
            }
        }

        static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
                source = VisualTreeHelper.GetParent(source);

            return source;
        } 






    }





  //public static class CommandMouseDoubleClick 
  //{    
  //    #region TheCommandToRun      
  //    /// <summary>     
  //    /// TheCommandToRun : 
  //    /// The actual ICommand to run   
  //    /// /// </summary>  
  //     public static readonly DependencyProperty TheCommandToRunProperty =  DependencyProperty.RegisterAttached("TheCommandToRun", typeof(ICommand),         typeof(CommandMouseDoubleClick),         new FrameworkPropertyMetadata((ICommand)null));  
  //    /// <summary>       /// Gets the TheCommandToRun property.         /// </summary>       
  //    public static ICommand GetTheCommandToRun(DependencyObject d)   
  //    {     
          
  //        return (ICommand)d.GetValue(TheCommandToRunProperty);    
      
      
  //    }     
  //    /// <summary>       /// Sets the TheCommandToRun property.     /// </summary>   
  //    /// 
  //    public static void SetTheCommandToRun(DependencyObject d, ICommand value)   
  //    {         d.SetValue(TheCommandToRunProperty, value);   
      
  //    }    
  //    #endregion   
      
  //    #region RoutedEventName     
  //    /// <summary>      
  //    /// /// RoutedEventName : The event that should actually execute the     
  //    /// /// ICommand     
  //    /// /// </summary>   
  //  public static readonly DependencyProperty RoutedEventNameProperty =    DependencyProperty.RegisterAttached("RoutedEventName", typeof(String),         typeof(CommandMouseDoubleClick),         new FrameworkPropertyMetadata((String)String.Empty,             new PropertyChangedCallback(OnRoutedEventNameChanged)));    
  //    /// <summary>       /// Gets the RoutedEventName property.      /// </summary>   
  //   public static String GetRoutedEventName(DependencyObject d)     
  //   {         return (String)d.GetValue(RoutedEventNameProperty);     }     
  //    /// <summary>       /// Sets the RoutedEventName property.     /// </summary>     
  //   public static void SetRoutedEventName(DependencyObject d, String value)    
  //   {    
  //       d.SetValue(RoutedEventNameProperty, value);   
  //   }     
  //    /// <summary>    
  //    /// /// Hooks up a Dynamically created EventHandler (by using the     /// <see cref="MouseDoubleClickEventHooker">
  //    /// MouseDoubleClickEventHooker</see> class) that when    
  //    /// /// run will run the associated ICommand    
  //    /// /// </summary>    
  //    private static void OnRoutedEventNameChanged(DependencyObject d,         DependencyPropertyChangedEventArgs e)  
  //    {       
  //        String routedEvent = (String)e.NewValue;    
  //        //If the RoutedEvent string is not null, create a new   
  //        //dynamically created EventHandler that when run will execute    
  //        //the actual bound ICommand instance (usually in the ViewModel)      
  //        if (!String.IsNullOrEmpty(routedEvent))      
  //        {  
  //            MouseDoubleClickEventHooker eventHooker = new MouseDoubleClickEventHooker();     
  //            eventHooker.ObjectWithAttachedCommand = d;    
  //            EventInfo eventInfo = d.GetType().GetEvent(routedEvent,      
  //                BindingFlags.Public | BindingFlags.Instance);       
  //            //Hook up Dynamically created event handler          
  //            if (eventInfo != null)       
  //            {         
  //                eventInfo.AddEventHandler(d,   
  //                    eventHooker.GetNewEventHandlerToRunCommand(eventInfo));    
  //            }       
  //        }   
  //    }   
  //    #endregion } 
  //    /// <summary> 
  //    /// /// Contains the event that is hooked into the source RoutedEvent
  //    /// /// that was specified to run the ICommand  /// </summary> 
  //    sealed class MouseDoubleClickEventHooker
  //    {    
  //        #region Properties    
  //        /// <summary>   
  //        /// /// The DependencyObject, that holds a binding to the actual      
  //        /// ICommand to execute    
  //        /// /// </summary>   
  //        public DependencyObject ObjectWithAttachedCommand 
  //        { get; set; } 
  //        #endregion    
  //        #region Public Methods   
  //        /// <summary>      
  //        /// Creates a Dynamic EventHandler that will be run the ICommand     
  //        /// /// when the user specified RoutedEvent fires     
  //        /// /// </summary>     /// <param name="eventInfo">The specified RoutedEvent EventInfo</param>   
  //        /// ///<returns>An Delegate that points to a new EventHandler   
  //        /// /// that will be run the ICommand</returns>  
  //        public Delegate GetNewEventHandlerToRunCommand(EventInfo eventInfo)   
  //        {      
  //            Delegate del = null;      
  //            if (eventInfo == null)     
  //                throw new ArgumentNullException("eventInfo");    
  //            if (eventInfo.EventHandlerType == null)     
  //                throw new ArgumentException("EventHandlerType is null");
  //            if (del == null)   
  //                del = Delegate.CreateDelegate(eventInfo.EventHandlerType, this,      
  //                    GetType().GetMethod("OnEventRaised",    
  //                    BindingFlags.NonPublic |                 BindingFlags.Instance));       
  //            return del;     }   
  //        #endregion   

  //        #region Private Methods   
  //        /// <summary>   
  //        /// /// Runs the ICommand when the requested RoutedEvent fires   
  //        /// /// </summary>   
  //        private void OnEventRaised(object sender, EventArgs e)  
  //        {         ICommand command = (ICommand)(sender as DependencyObject).      
  //            GetValue(CommandMouseDoubleClick.TheCommandToRunProperty);    
  //            if (command != null)    
  //            {       
  //                command.Execute(null);      
  //            }   
  //        }  



  //        #endregion
      
      
  //    } 
      
  //}
      
      //The xaml could look something like below,




    //public static class TreeViewItemBehavior
    //{
    //    #region IsBroughtIntoViewWhenSelected

    //    public static bool GetIsBroughtIntoViewWhenSelected(TreeViewItem treeViewItem)
    //    {
    //        return (bool)treeViewItem.GetValue(IsBroughtIntoViewWhenSelectedProperty);
    //    }

    //    public static void SetIsBroughtIntoViewWhenSelected(TreeViewItem treeViewItem, bool value)
    //    {
    //        treeViewItem.SetValue(IsBroughtIntoViewWhenSelectedProperty, value);
    //    }

    //    public static readonly DependencyProperty IsBroughtIntoViewWhenSelectedProperty =
    //        DependencyProperty.RegisterAttached(
    //        "IsBroughtIntoViewWhenSelected",
    //        typeof(bool),
    //        typeof(TreeViewItemBehavior),
    //        new UIPropertyMetadata(false, OnIsBroughtIntoViewWhenSelectedChanged));

    //    static void OnIsBroughtIntoViewWhenSelectedChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    //    {
    //        TreeViewItem item = depObj as TreeViewItem;
    //        if (item == null)
    //            return;

    //        if (e.NewValue is bool == false)
    //            return;

    //        if ((bool)e.NewValue)
    //        {

    //            item.MouseDoubleClick += new MouseButtonEventHandler(item_MouseDoubleClick);
    //            item.Selected += OnTreeViewItemSelected;
    //        }
    //        else
    //        {
    //            item.MouseDoubleClick += new MouseButtonEventHandler(item_MouseDoubleClick);
    //            item.Selected -= OnTreeViewItemSelected;

    //        }
    //    }

    //    static void item_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    //    {
    //        // Only react to the Selected event raised by the TreeViewItem
    //        // whose IsSelected property was modified.  Ignore all ancestors
    //        // who are merely reporting that a descendant's Selected fired.
    //        if (!Object.ReferenceEquals(sender, e.OriginalSource))
    //            return;

    //        TreeViewItem item = e.OriginalSource as TreeViewItem;



           
    //    }

    //    static void OnTreeViewItemSelected(object sender, RoutedEventArgs e)
    //    {
    //        // Only react to the Selected event raised by the TreeViewItem
    //        // whose IsSelected property was modified.  Ignore all ancestors
    //        // who are merely reporting that a descendant's Selected fired.
    //        if (!Object.ReferenceEquals(sender, e.OriginalSource))
    //            return;

    //        TreeViewItem item = e.OriginalSource as TreeViewItem;
    //        if (item != null)
    //            item.BringIntoView();
    //    }

    //    #endregion // IsBroughtIntoViewWhenSelected
 

    //}
}
